package QC.MessageBox.common;

import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Andaren on 2017/5/13.
 * Used for: 定时任务
 */
public class PeriodicTime {

    private static final Calendar PERIODIC_TASK_CALENDAR = Calendar.getInstance();

    /**
     * 这里注意 SimpleDateFormat是 非线程安全 的
     */
    private static final SimpleDateFormat DATEFORMAT_YYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat DATEFORMAT_YYYYMMDDHHMMSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat DATEFORMAT_YYYYMMDDHHMMSS_SSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    public static final String DAYTIME_MAX = " 23:59:59";
    public static final String DAYTIME_MIN = " 00:00:00";

    public static boolean isEndOfNight() {
        PERIODIC_TASK_CALENDAR.setTime(new Date());
        int hour = PERIODIC_TASK_CALENDAR.get(Calendar.HOUR_OF_DAY);
        int minute = PERIODIC_TASK_CALENDAR.get(Calendar.MINUTE);
        if (hour == 0 && minute == 0) {
            return  true;
        }
        return false;
    }

    /**
     * 获取30天前的零点时间字符串
     * @param now
     * @return
     */
    public static String getDateStrTheDayBeforeAMonth(String now) {
        String monthDayBeforeStr = "";
        try {
            Date nowDate = DateUtils.parseDate(now, "yyyy-MM-dd");
            Date sevenDayBefore = DateUtils.addDays(nowDate, -30 * 12);
            monthDayBeforeStr = PeriodicTime.DATEFORMAT_YYYYMMDD.format(sevenDayBefore);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return monthDayBeforeStr;
    }

    /**
     * 30天之前的时间戳
     * @return
     */
    public static Long get30DaysBeforeTimestamp() {
        Long currentTimestamp = System.currentTimeMillis() / 1000;
        Long distance = Long.valueOf(30 * 24 * 3600);
        return currentTimestamp - distance;
    }

    /**
     * 返回当前时间字符串
     * @return
     */
    public static String getCurrentTimeStr(String pattern) {
        DateFormat format = new SimpleDateFormat(pattern);
        return format.format(new Date());
    }

    public static String getCurrentDate() {
        return DATEFORMAT_YYYYMMDD.format(new Date().getTime());
    }

    public static String getCurrentTimeByyyyMMddHHmmss() {
        return DATEFORMAT_YYYYMMDDHHMMSS.format(new Date());
    }

    public static String getCurrentTimeByyyyMMddHHmmssSSS() {
        return DATEFORMAT_YYYYMMDDHHMMSS_SSS.format(new Date());
    }

    /**
     * 获取当前时间戳（秒）
     * @return
     */
    public static Long getCurrentTimestamp() {
        return (System.currentTimeMillis() / 1000);
    }

    public static String getCurrentTimestampStr() {
        return String.valueOf(getCurrentTimestamp());
    }

    public static void main(String args[]) throws Exception {
        System.out.println(getCurrentTimestamp());
    }
}
